<template>
	<view class="u-page">		
		<view class="qrcode">
			<image :src="qrcode" style="width: 340rpx;height: 340rpx;"></image>
			<view class="driver">请让用户扫码，可以直接获取微信</view>
		</view>
		<view class="x-search">
			<u-search shape="square" @search="search" @clear="search" :clearabled="true" :showAction="false" placeholder="请输入微信昵称/手机号" v-model="keywords"></u-search>
		</view>
		<view v-if="list.length>0" v-for="(item, index) in list" :key="index">
			<u-cell @click="clickUser(item)" :title="item.nickname" :isLink="true">
				<u-avatar
					slot="icon"
					shape="square"
					size="35"
					:src="item.avatar"
					customStyle="margin: -3px 5px -3px 0"
				></u-avatar>
			</u-cell>
		</view>
		<u-empty v-else text="没有找到微信账号!" mode="search" marginTop="50"></u-empty>
	</view>
</template>

<script>
	import {baseUrl,methods} from '@/utils/methods.js';
	import {rand} from '@/utils/util.js';
	export default {
		data() {
			return {
				keywords:'',
				list: [],
				qrcode:'',
				r:'',
				is_show:false
			}
		},
		onLoad() {
			this.pagelist();
			this.r=rand(10000,99999);
			let token=uni.getStorageSync('token');
			this.qrcode=baseUrl+"api/admin/admin/addWxuser?token="+token+"&foreign_key="+this.r
		},
		onShow(){
			this.is_show=true;
			this.getxuser();
		},
		onHide() {
			this.is_show=false;
		},
		onUnload() {
			this.is_show=false;
		},
		onReachBottom() {
			this.pagelist();
		},
		onPullDownRefresh:function(){
			this.list=[];
			this.pagelist(true);
		},
		filters:{
			parseAvatar:function(data){
				if(data.xuser){
					return data.xuser.avatar;
				}
				if(data.sex==1){
					return '/static/boy.png';
				}
				if(data.sex==2){
					return '/static/girl.png';
				}
			}
		},
		methods: {
			...methods,
			getxuser:function(){
				this.$get('admin/admin/getScanUser',{foreign_key:this.r}).then(res=>{
					if(res){
						uni.setStorageSync('check_user',res);
						uni.navigateBack();
					}
				}).catch(e=>{
					let that=this;
					setTimeout(function(){
						that.is_show && that.getxuser(); 
					},2000);
				});
			},
			clickUser:function(item){
				uni.setStorageSync('check_user',item);
				uni.navigateBack();
			},
			search:function(){
				if(this.keywords.trim()==''){
					this.$msg('请输入微信昵称或手机号搜索！');
					return;
				}
				this.pagelist();
			},
			pagelist:function(pulldown=false){
				let cate_id=this.current?this.cate[this.current-1].id:'';
				this.$get("common/wxuser",{
					keywords:this.keywords
				},true).then(res=>{
					if(pulldown){
						uni.stopPullDownRefresh();
					}
					this.list=res;			
				});		
			}
		}
	}
</script>

<style lang="scss">
	.x-search{
		padding:20rpx;
		background-color: #fff;
	}
	.qrcode{
		text-align: center;
		padding-top: 40rpx;
		.driver{
			margin: 10rpx;
			font-size: 24rpx;
		}
	}
</style>
